package org.micode.libs.smon.utils;

import org.micode.libs.smon.vo.JavaInfo;
import org.micode.libs.smon.vo.JvmInfo;
import org.micode.libs.smon.vo.JvmRuntimeInfo;

import java.util.Properties;

public class JvmUtils {

    public static JavaInfo readJavaInfo() {
        JavaInfo info = new JavaInfo();
        fillJava(info);
        return info;
    }

    public static void fillJava(JavaInfo info) {
        if (null == info) {
            return;
        }

        Properties props = System.getProperties();

        info.setVersion(props.getProperty("java.version"));
        info.setVendor(props.getProperty("java.vendor"));
        info.setVendorUrl(props.getProperty("java.vendor.url"));
        info.setJavaHome(props.getProperty("java.home"));
        info.setSpecVersion(props.getProperty("java.specification.version"));
        info.setSpecVendor(props.getProperty("java.specification.vendor"));
        info.setSpecName(props.getProperty("java.specification.name"));
        info.setClassVersion(props.getProperty("java.class.version"));
        info.setClassPath(props.getProperty("java.class.path"));
        info.setLibraryPath(props.getProperty("java.library.path"));
        info.setIoTmpDir(props.getProperty("java.io.tmpdir"));
        info.setExtDirs(props.getProperty("java.ext.dirs"));
    }

    public static JvmInfo readJvmInfo() {
        JvmInfo info = new JvmInfo();
        fillJvm(info);
        return info;
    }

    public static void fillJvm(JvmInfo info) {
        if (null == info) {
            return;
        }

        Properties props = System.getProperties();

        info.setVersion(props.getProperty("java.vm.version"));
        info.setVendor(props.getProperty("java.vm.vendor"));
        info.setName(props.getProperty("java.vm.name"));
        info.setSpecVersion(props.getProperty("java.vm.specification.version"));
        info.setSpecVendor(props.getProperty("java.vm.specification.vendor"));
        info.setSpecName(props.getProperty("java.vm.specification.name"));
    }

    public static JvmRuntimeInfo readJvmRuntime() {
        JvmRuntimeInfo info = new JvmRuntimeInfo();
        fillJvmRuntime(info);
        return info;
    }

    public static void fillJvmRuntime(JvmRuntimeInfo info) {
        if (null == info) {
            return;
        }

        Runtime r = Runtime.getRuntime();

        info.setTotalMemory(r.totalMemory());
        info.setFreeMemory(r.freeMemory());
        info.setAvailableProcessors(r.availableProcessors());
    }

}
